package com.hyjx.framework.common.util;

import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * 字符串处理帮助类
 */
public class StringHelper {
    /**
     * 获取uuid类型的字符串
     *
     * @return uuid字符丿
     */
    public static String getPrimaryKey() {
        return java.util.UUID.randomUUID().toString().replaceAll("-", "");
    }

    /**
     * 判断字符串是否为穿
     *
     * @param str 字符丿
     * @return 是否为空标识
     */
    public static boolean isEmpty(String str) {
        return str == null || str.length() == 0;
    }

    /**
     * 判断字符串是否为非空
     *
     * @param str 字符丿
     * @return 是否为非空标诿
     */
    public static boolean isNotEmpty(String str) {
        return !isEmpty(str);
    }

    /**
     * 根据字符串数组返回?号分隔的字符串倿
     *
     * @param strArray 字符串数绿
     * @return 逗号分隔的字符串
     */
    public static String getStringByArray(String... strArray) {
        if (strArray == null)
            return "";
        StringBuilder buffer = new StringBuilder(strArray.length * 10);
        for (String str : strArray) {
            buffer.append(str).append(",");
        }
        buffer.deleteCharAt(buffer.length() - 1);
        return buffer.toString();
    }

    /**
     * xml内容特殊符号替换
     *
     * @param xml xml字符丿
     * @return 替换后的xml
     */
    public static String textXML(String xml) {
        if (xml == null)
            return "";
        String content = xml;
        content = content.replaceAll("<", "&lt;");
        content = content.replaceAll(">", "&gt;");
        content = content.replaceAll("\"", "&quot;");
        content = content.replaceAll("\n", "</br>");
        return content;
    }

    /**
     * 构?排序条仿
     *
     * @param order
     *            排序方向
     * @param orderby
     *            排序字段
     * @return 组装好的排序sql
     */

    /**
     * 箿单字符串匹配方法，支持匹配类型为＿ *what *what* what*
     *
     * @param pattern 匹配模式
     * @param str     字符丿
     * @return 是否匹配
     */
    public static boolean simpleMatch(String pattern, String str) {
        if (pattern == null || str == null) {
            return false;
        }
        int firstIndex = pattern.indexOf('*');
        if (firstIndex == -1) {
            return pattern.equals(str);
        }
        if (firstIndex == 0) {
            if (pattern.length() == 1) {
                return true;
            }
            int nextIndex = pattern.indexOf('*', firstIndex + 1);
            if (nextIndex == -1) {
                return str.endsWith(pattern.substring(1));
            }
            String part = pattern.substring(1, nextIndex);
            int partIndex = str.indexOf(part);
            while (partIndex != -1) {
                if (simpleMatch(pattern.substring(nextIndex), str.substring(partIndex + part.length()))) {
                    return true;
                }
                partIndex = str.indexOf(part, partIndex + 1);
            }
            return false;
        }
        return (str.length() >= firstIndex && pattern.substring(0, firstIndex).equals(str.substring(0, firstIndex))
                && simpleMatch(pattern.substring(firstIndex), str.substring(firstIndex)));
    }

    /**
     * 返回标准日期格式字符串
     */
    public static String getDate() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(new Date());
    }

    /**
     * 返回标准日期时间格式字符串
     */
    public static String getDateTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(new Date());
    }

    /**
     * 返回标准日期时间格式字符串
     */
    public static String getDateTimeMill() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
        return sdf.format(new Date());
    }
}
